/**
 * 页面权限筛选执行器
 */
layui.define(['jquery', 'conf', 'api'], function (exports) {
    const { jquery: $, conf, api } = layui;
    const userRole = conf.authority.getInfo();
    var _data;

    exports('authority', {
        roleName() {
            return userRole.name;
        },
        run: function () {
            if (userRole.auths) {
                $("[lay-auth]").each(function (idx, elem) {
                    if (!$.inArray($(elem).attr("lay-auth"), userRole.auths)) {
                        $(elem).remove();
                    }
                });
            }


            $("[lay-role]").each(function (idx, elem) {
                var has = false;
                var isIgnore;

                var role = $(elem).attr("lay-role");
                if (role.substr(0, 1) == "!") {
                    //指定角色都不显示
                    isIgnore = true;
                } else {
                    //指定角色显示
                    isIgnore = false
                }
                var roles = role.substr(1).split("|");

                for (let r of roles) {
                    if (r != roleName) {
                        continue;
                    }

                    has = true;
                    break;
                }

                if (has && isIgnore) {
                    $(elem).remove();
                } else if (!has && !isIgnore) {
                    $(elem).remove();
                }
            });

        },
        check: function (checkName) {
            return checkName === this.roleName();
        },
        /**
         * 获取源信息
         */
        getOriginData: async function () {
            if (_data) {
                return _data;
            }

            var modules = {};
            await api.json("/menu.json").then(function (res) {
                _data = res;
                res.forEach(menu => {
                    (menu.children || []).map(function (module) {
                        modules[module] = [];
                    });
                });
                //组合动态获取view里的authority.json文件
            });
            //获取所有信息
            await Promise.all(Object.keys(modules).map(function (module) {
                return new Promise(function (resolve) {
                    api.json('/view/' + module + "/authority.json").then(function (items) {
                        modules[module] = items;
                        resolve();
                    });
                });
            }));

            //替换内容
            _data.forEach(menu => {
                if ($.isArray(menu.children)) {
                    var moduleMenuList = [];
                    menu.children.forEach(module => {
                        modules[module].map(function (menu) {
                            moduleMenuList.push(menu);
                        });
                    });
                    menu.children = moduleMenuList;
                }
            });

            return _data;
        },
        menus: function () {
            const that = this;
            return new Promise(async function (resolve) {
                var filterNoPageMenuItems = function (nodeList) {
                    var newNodeList = [];
                    nodeList.forEach(node => {
                        if (["page", "menu", "link"].indexOf(node.type) != -1) {
                            newNodeList.push(node);
                            if (node.children) {
                                node.children = filterNoPageMenuItems(node.children);
                            }
                        }
                    });
                    return newNodeList;
                }

                var auths = await that.getOriginData();
                resolve(filterNoPageMenuItems(auths));
            });
        }
    });
});